package com.softserveinc.moviedbapp.model;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Movie implements Parcelable {

	private static String TAG = Movie.class.getSimpleName();

	private double rating;
	private int movieId;
	private int votes;
	private String name;
	private String certification;
	private Date released;
	private String thumbPosterUrl;
	private String lastModifiedAt;
	private boolean posterRecycled = true;
	private Bitmap posterBitmap;
	private String url;

	static SimpleDateFormat FORMATTER = new SimpleDateFormat("yyyy-MM-dd");

	// constructor
	public Movie() {

	}

	/* create obj from parcel */
	private Movie(final Parcel in) {
		movieId = in.readInt();
		rating = in.readDouble();
		name = in.readString();
		certification = in.readString();
		setReleased(in.readString());
		thumbPosterUrl = in.readString();
		url = in.readString();

		Bundle posterBundle = in.readBundle();
		posterBitmap = posterBundle.getParcelable("posterBitmap");
	}

	public int getMovieId() {
		return movieId;
	}

	public void setMovieId(final int movieId) {
		this.movieId = movieId;
	}

	public int getVotes() {
		return votes;
	}

	public void setVotes(final int votes) {
		this.votes = votes;
	}

	public double getRating() {
		return rating;
	}

	public void setRating(final double rating) {
		this.rating = rating;
	}

	public String getCertification() {
		return certification;
	}

	public void setCertification(final String certification) {
		this.certification = certification.trim();
	}

	public String getName() {
		return name;
	}

	public void setName(final String name) {
		this.name = name.trim();
	}

	public String getReleased() {
		if (released != null) {
			return FORMATTER.format(released);
		}
		return null;
	}

	public void setReleased(final String released) {
		try {
			this.released = FORMATTER.parse(released.trim());
		} catch (Exception e) {
			try {
				this.released = FORMATTER.parse("1970-01-01");
			} catch (ParseException e1) {
				Log.e(TAG, "Date parse exception ", e1);
			}
		}
	}

	public String getLastModifiedAt() {
		return lastModifiedAt;
	}

	public void setLastModifiedAt(final String lastModifiedAt) {
		this.lastModifiedAt = lastModifiedAt;
	}


	public String getThumbPosterUrl() {
		return thumbPosterUrl;
	}


	public void setThumbPosterUrl(final String link) {
		thumbPosterUrl = link;
	}


	public String getUrl() {
		return url;
	}

	public void setUrl(final String url) {
		this.url = url;
	}

	/**
	 * @return the posterRecycled
	 */
	public boolean isPosterRecycled() {
		return posterRecycled;
	}

	/**
	 * @param posterRecycled the posterRecycled to set
	 */
	public void setPosterRecycled(final boolean posterRecycled) {
		this.posterRecycled = posterRecycled;
	}

	public Bitmap getPosterBitmap() {
		return posterBitmap;
	}

	public void setPosterBitmap(final Bitmap posterBitmap) {
		this.posterBitmap = posterBitmap;
	}

	// creates a copy of this object
	public Movie copy() {
		Movie copy = new Movie();
		copy.certification = certification;
		copy.name = name;
		copy.movieId = movieId;
		copy.rating = rating;
		copy.released = released;
		copy.lastModifiedAt = lastModifiedAt;
		copy.url = url;
		copy.thumbPosterUrl = thumbPosterUrl;
		copy.votes = votes;
		return copy;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("\nTitle: ");
		sb.append(name);
		sb.append(", ID: ");
		sb.append(movieId);

		return sb.toString();
	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(final Parcel dest, final int flags) {
		dest.writeInt(movieId);
		dest.writeDouble(rating);
		dest.writeString(name);
		dest.writeString(certification);
		dest.writeString(getReleased());
		dest.writeString(thumbPosterUrl);
		dest.writeString(url);

		Bundle posterBundle = new Bundle();
		posterBundle.putParcelable("posterBitmap", posterBitmap);
		dest.writeBundle(posterBundle);
	}

	public static final Parcelable.Creator<Movie> CREATOR = new Parcelable.Creator<Movie>() {
		@Override
		public Movie createFromParcel(final Parcel in) {
			return new Movie(in);
		}

		@Override
		public Movie[] newArray(final int size) {
			return new Movie[size];
		}
	};

}
